<template>
  <basic-container>
    <avue-crud
        v-if="tableOption"
        v-bind="bindVal"
        v-on="onEvent"
        v-model:page="page"
        v-model="startForm">
      <template slot="menuLeft">
        <el-button type="primary" size="small">暂停全部任务</el-button>
        <el-button type="primary" size="small">恢复全部任务</el-button>
      </template>

      <template #menu-btn="{type,size,row}">
        <el-dropdown-item icon="el-icon-edit" v-if="row.isEnable===0" :size="size" @click.native="activeTask(row)"
                          :type="type">激活任务
        </el-dropdown-item>

        <el-dropdown-item v-if="row.isEnable===1&&['WAITING'].includes(row.triggerState)"
                          icon="el-icon-video-pause"
                          :size="size" @click.native="pauseTask(row)"
                          :type="type">暂停任务
        </el-dropdown-item>
        <el-dropdown-item v-if="row.isEnable===1 &&['PAUSED'].includes(row.triggerState)" icon="el-icon-edit"
                          :size="size" @click.native="recoveryTask(row)"
                          :type="type">恢复任务
        </el-dropdown-item>
        <el-dropdown-item v-if="row.isEnable===1" icon="el-icon-video-play"
                          :size="size" @click.native="startTask(row)"
                          :type="type">立即执行
        </el-dropdown-item>
        <el-dropdown-item v-if="row.isEnable===1"
                          icon="el-icon-edit"
                          :size="size"
                          @click.native="disableTask(row)"
                          :type="type">停止任务
        </el-dropdown-item>
        <el-dropdown-item icon="el-icon-search"
                          :size="size"
                          @click.native="taskLog(row)"
                          :type="type">任务日志
        </el-dropdown-item>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>

import {activeById, disableById, pauseById, recoveryById, startById} from "@/api/start/quartz/quartzApi";


import {createDynamicMixin} from "@/mixins/startCrud.js"
import autoRouter from "./autoRouter";
import {deepClone} from "utils/util";

const option = {}
option.name = "start/quartz/quartz"
option.permissionBtnHandler = {

  delBtn: (vm, row, defaultValue) => {
    return 0 === row.isEnable
  }
}
export default {
  mixins: [createDynamicMixin(option)],

  methods: {
    beforeRenderDataSource(data) {
      let contextMapData;
      for (let i = 0; i < data.length; i++) {
        contextMapData = data[i].taskEnvData || {}
        data[i].taskEnvData = JSON.stringify(contextMapData, null, "\t")
      }
    },
    rowSaveBefore(row) {
      row = deepClone(row);
      let contextMapData = deepClone(row.taskEnvData || "{}");
      row.taskEnvData = JSON.parse(contextMapData)
      return row;
    },
    beforeRowUpdate(row) {
      row = deepClone(row);
      let contextMapData = deepClone(row.taskEnvData || "{}");
      row.taskEnvData = JSON.parse(contextMapData)
      return row;
    },
    taskLog(row) {
      let title = row.taskName + "-任务日志"
      this.$router.push({path: autoRouter.names.quartz.log.path + "/" + row.id + "/" + title})
      return true;
    },
    activeTask(row) {
      this.$confirm(`是否激活名称为【${row.taskName}】的任务?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return activeById(row.id)
      }).then(res => {
        this.$message.success("激活任务成功");
        this.refreshChange()
      })
    },
    disableTask(row) {
      this.$confirm(`是否停用名称为【${row.taskName}】的任务?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return disableById(row.id)
      }).then(res => {
        this.$message.success("停用任务成功");
        this.refreshChange()
      })
    },
    pauseTask(row) {
      this.$confirm(`是否暂停名称为【${row.taskName}】的任务?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return pauseById(row.id)
      }).then(res => {
        this.$message.success("暂停任务成功");
        this.refreshChange()
      })
    },
    recoveryTask(row) {
      this.$confirm(`是否恢复名称为【${row.taskName}】的任务?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return recoveryById(row.id)
      }).then(res => {
        this.$message.success("恢复任务成功");
        this.refreshChange()
      })
    },
    startTask(row) {
      this.$confirm(`是否立即执行名称为【${row.taskName}】的任务?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return startById(row.id)
      }).then(res => {
        this.$message.success("立即执行任务成功");
        this.refreshChange()
      })
    },

  }
}
</script>

<style scoped>

</style>
